package Scenery.Map;

import Scenery.BaseBuilder;
import Scenery.BaseBuilder;
import Scenery.BaseCharacter;
import Scenery.BaseCharacter;
import Scenery.Map.Dungeon.Cell;
import Scenery.Map.Dungeon.Dungeon;
import Scenery.Thing.Things.Entity;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import java.util.LinkedList;
import java.util.List;
import jme3tools.optimize.GeometryBatchFactory;
import main.Memory.Memory;

/*
 * 
 * @author: Ziden
 * 
 */

public class MapControl {

   
    public MapControl(BaseBuilder builder) {
        this.builder = builder;
    }

    private Map map;
    private BaseCharacter dude;
    BaseBuilder builder;

    public Map getMap() {
        return map;
    }
    
    public void addEntity(Entity e, Vector3f position) {
        e.getEntityMasterNode().setLocalTranslation(position);
        e.getEntityMasterNode().addControl(new RigidBodyControl(0));
        builder.getSpace().add(e.getEntityMasterNode());
        map.addObjectNode(e.getEntityMasterNode());
    }
    
    public void makeTestMap() {
        
        map = new Map(50, 50);
        for(int x = 0 ; x < 50 ; x++) {
           for(int y = 0 ; y < 50 ; y++) {
              map.getMap()[x][y]=new Tile(TypeDefs.grass); 
           }
       }
       map.buildMapModel();
       builder.createRigidMapPhysics(map.getStaticMap());
       dude = builder.makeBaseCharacter();
        /*
       map = new Map(50,50);
       Dungeon dungeon = new Dungeon(50,50,0);
       Cell [][] cells = dungeon.getCells();
       for(int x = 0 ; x < 50 ; x++) {
           for(int y = 0 ; y < 50 ; y++) {
              map.getMap()[x][y]=new Tile(cells[x][y].getType()); 
           }
       }
       //
       //map.getMap()[3][3].setObject(TypeDefs.SIGN);
       //map.getMap()[4][4].setObject(TypeDefs.PLANK);
       map.buildMapModel();
       builder.createRigidMapPhysics(map.getStaticMap());
       builder.createThingsPhysics(map.getThings());
       dude = builder.makeBaseCharacter();
       //GeometryBatchFactory.optimize(dude.getModel());
       //dude.getMasterNode().setLocalTranslation(dungeon.startX, 0, dungeon.startY);
         * 
         */
    }
    
    public void click(Geometry g) {
        if(g.getName().equals("Crate")) {
            System.out.println("Clicou na caixa");
            dude.pickUp(g);
        }
        if(g.getName().length() > 4) {
            if(g.getName().substring(0, 4).equalsIgnoreCase("Sign")) {
                Integer signId = Integer.valueOf(g.getName().substring(5));
                System.out.println("Clicou na sign "+signId);
                if(Memory.getSigns().containsKey(signId)) {
                    String [] text = Memory.getSigns().get(signId);
                    if(text != null) {
                        dude.setReadingSignId(signId);
                    }
                }
            }
        }
    }
    
    public BaseCharacter getDude() { return dude; }
    
    public void pickAction(Geometry target) {
        
    }
    
}
